﻿using Microsoft.MixedReality.Toolkit.Experimental.UI;
using Microsoft.MixedReality.Toolkit.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
    public class SearchMenu : MonoBehaviour
    {
       // [SerializeField]
       // private ScrollingObjectCollection _scrolling;
        [SerializeField]
        private SearchItem _phototype;
        [SerializeField]
        private GridObjectCollection _collection;
        //  [SerializeField]
        //  private ClippingBox _clipping;
        [SerializeField]
        private Transform _parent;
        private List<SearchItem> list_ = new List<SearchItem>();
        public void clear() {

            foreach (SearchItem item in list_) {
                GameObject.DestroyImmediate(item.gameObject);
            }
            list_.Clear();
        }
       
        public void setup(SearchModel.DataSyncList datas, Action<int> callback) {
          //  Debug.LogError("setup");
            clear();
            //_clipping.ClearRenderers();
            if (datas != null && datas.Count != 0)
            {
                for (int i = 0; i < datas.Count; ++i) {
                    SearchItem item = GameObject.Instantiate(_phototype);
                    list_.Add(item);
                    item.setup(i, datas[i]);
                    item.transform.SetParent(_parent, false);
                    /*Renderer[] renderers = item.GetComponentsInChildren<Renderer>(true);
                    foreach (var renderer in renderers)
                    {
                        _clipping.AddRenderer(renderer);
                    }*/
                    item.onClicked += callback;
                    item.gameObject.SetActive(true);
                }
            }
            _collection.UpdateCollection();
          //  _scrolling.UpdateCollection();

        }
        private int selected_ = -1;
        internal void release() {
            if (selected_ != -1) { 
                list_[selected_].release();
                selected_ = -1;
            }
        }
        internal void selected(int selected)
        {
            Debug.LogError(selected);
            if (selected != -1) { 
                selected_ = selected;
                list_[selected].selected();
            }
        }

      
    }
}
